using System;
using System.Collections.Generic;
using System.Text;
using System.Data .OleDb ;
using DTO;
using System.Data ;
namespace DAO
{
    public class LoaiBangCapDAO
    {   //ham lay dsach laoi bang cap tu csdl len
        public List<LoaiBangCapDTO> laydanhsachbangcap_DAO()
        {  //khai bao 1 list cos kieu du lieu laobangcapdto
            List<LoaiBangCapDTO> list = new List<LoaiBangCapDTO>();
            //cau lenh sql de lay du lieu tu csdl len
            string sql = "select * from BangCap ";
            //thuc thi cau truy van sql bo vao datatable 
            DataTable dt = DataProvider.ExcuteQuery(sql);
            //kiem tra trong datatable dong co du lieu k?
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {   //lay 1 dong trong datatable bo vo datarow
                    DataRow dr = dt.Rows[ i];
                    //gan thuoc tinh Mabangcap trong datarow cho bieb mabangcap
                    int mabangcap = int.Parse(dr["MaBangCap"].ToString());
                    string  tenbangcap = dr["TenBangCap"].ToString();
                    //khai bao moi 1 phan tu co kieu du lieu laoibangcapdto
                    LoaiBangCapDTO lbc = new LoaiBangCapDTO();
                    //gan thuoc tinh maloaibangcap trong datarow vao maloaibangcap cua phan tu laoibangcapdto
                    lbc.Mabangcap = mabangcap;
                    lbc.Tenbangcap = tenbangcap;
                    //add phan tu loaibangcap vao list loaibangcap
                    list.Add(lbc);
                }
            }
            return list;
        }

        public LoaiBangCapDTO   laytentheoma_DAO(int mabangcap)
        {
            LoaiBangCapDTO  lbc = new LoaiBangCapDTO ();
            string sql = "select * from BangCap where MaBangCap = " + mabangcap;
            DataTable dt = DataProvider.ExcuteQuery(sql);
            DataRow dr = dt.Rows[0];
            string tenbangcap = dr["TenBangCap"].ToString();
            lbc .Tenbangcap  = tenbangcap;
            return lbc;
        }

        public void ThemDAO(LoaiBangCapDTO lbc)
        {
            string sql = "insert into BangCap (TenBangCap)  values('" + lbc.Tenbangcap + "')";
            DataProvider .ExecutenonQuery(sql);
        }
    }
}
